package com.qiku.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiku.reggie.common.CustomerException;
import com.qiku.reggie.mapper.CategoryMapper;
import com.qiku.reggie.mapper.EmployeeMapper;
import com.qiku.reggie.pojo.Category;
import com.qiku.reggie.pojo.Dish;
import com.qiku.reggie.pojo.Employee;
import com.qiku.reggie.pojo.Setmeal;
import com.qiku.reggie.service.CategoryService;
import com.qiku.reggie.service.DishService;
import com.qiku.reggie.service.EmployeeService;
import com.qiku.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;


    @Autowired
    private SetmealService setmealService;


    @Override
    public void remove(Long id) {
        // 1 查询 dish菜品表 select count(*) from dish where category_id=?
        QueryWrapper<Dish> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("category_id" , id);
        int count1 = dishService.count(queryWrapper1);
        if (count1 > 0){
            // 抛出异常
            throw  new CustomerException("不能删除分类，有相关联的菜品数据");

        }
        // 2 查询setmeal 套餐表  select count(*) from setmeal where category_id=?
        QueryWrapper<Setmeal> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("category_id" , id);
        int count2 = setmealService.count(queryWrapper2);
        if (count2 > 0){
            // 抛出异常
            throw  new CustomerException("不能删除分类，有相关联套餐数据");
        }
        // 3 删除 category 分类表  delete from category where id =?

        this.removeById(id);
    }
}
